<?php
if (!session_id()) {
    session_start();
}
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

  require 'src/facebook.php';  
  global $wpdb;
  if(isset($_SESSION['user_id']))
	$xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");
  
  if(!(isset($_SESSION['user_id']) && $_SESSION['user_id']) || (isset($_SESSION['user_id']) && !isset($xuser->id) )){ 

  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
  'appId' => '167870453400890',
  'secret' => '40e14ed4043dde1a6d4c284c4ec21a0d',
  ));

  // Get User ID
  $user = $facebook->getUser();


  // We may or may not have this data based on whether the user is logged in.
  //
  // If we have a $user id here, it means we know the user is logged into
  // Facebook, but we don't know if the access token is valid. An access
  // token is invalid if the user logged out of Facebook.

  if ($user) {
  try {
  // Proceed knowing you have a logged in user who's authenticated.
  $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
  error_log($e);
  $user = null;
  }
  } else {
  $loginUrl = $facebook->getLoginUrl();
  echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
  }

  $_SESSION['user_id'] = $user;

  }
  if(!isset($_SESSION['user_id']) && !is_home()){
		wp_redirect( home_url() ); exit;
  }
  
  $xuser = $wpdb->get_row("SELECT * FROM ap_painting_user WHERE  user_id = {$_SESSION['user_id']}");
  
 /*
if (is_home() && $_SESSION['user_id'] ) {
    $location = get_site_url() . '/the-le';
    wp_redirect($location);
} */
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="vi" xml:lang="vi">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <meta charset="<?php bloginfo('charset'); ?>" />

        <title> <?php wp_title('|', true, 'right'); ?></title>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/jquery-ui.js"></script>
        <script type="text/javascript" src="<?php echo bloginfo('template_url'); ?>/js/slimScroll.js"></script>
        <link href="<?php echo bloginfo('template_url'); ?>/css/style.css" type="text/css" rel="stylesheet" />

        <?php //wp_head();   ?>
        
        <script type="text/javascript">
            var base_url = '<?php echo bloginfo('template_url'); ?>/';

        </script>
    </head>

    <body>
	<div id="fb-root"></div>
        <div class="container <?php echo is_home() ? 'intro' : ''; ?> <?php echo $_SESSION['user_id'] && isset($xuser ->id )  ? 'login' : '' ?>">
            <div class="header">
                <a href="<?php echo site_url() ?>"><img class="logo1" src="<?php echo bloginfo('template_url'); ?>/img/cream.png"></a>
                <a href="<?php echo site_url() ?>"><img class="logo2" src="<?php echo bloginfo('template_url'); ?>/img/smalltalent.png"></a>
            </div>
            <div class="main">
                <div class="menu">
                    <ul>
                        <li class="home <?php echo is_home() ? 'active' : ''; ?>"  onclick="document.location.href = '<?php echo site_url() ?>'"><a href="javascript:void(0)">&nbsp;</a></li>
                        <li class="thele <?php echo is_page('the-le') ? 'active' : ''; ?>"  onclick="document.location.href = '<?php echo site_url() ?>/the-le'"><a href="javascript:void(0)">&nbsp;</a></li>
                        <li class="hocnghe <?php echo is_page('hoc-nghe') || is_page('album') || is_page('edit-img') ? 'active' : ''; ?>" onclick="document.location.href = '<?php echo site_url() ?>/hoc-nghe'"><a href="javascript:void(0)">&nbsp;</a></li>
                        <li class="tranhtai <?php echo is_page('tranh-tai') || is_page('exam') ? 'active' : ''; ?>" onclick="document.location.href = '<?php echo site_url() ?>/tranh-tai'"><a href="javascript:void(0)">&nbsp;</a></li>


                    </ul>
                    <div style="clear:both; "></div>
                </div>
                
                <?php if (!(is_page('the-le') || is_home())) { ?>
                    <div id="addimg"><img class="" src="<?php echo bloginfo('template_url'); ?>/img/add.png"></div>
                <?php } ?>